package cat.joronya.supermanager.service;

import java.util.ArrayList;
import java.util.List;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

import android.util.Log;

import cat.joronya.supermanager.Supermanager;
import cat.joronya.supermanager.market.Player;

/**
 * Donat l'HTML del llistat de mercat per posicio, el parsejem i
 * retornem els jugadors en VO's.
 * 
 * @author pol
 */
public class MarketBuyParser 
{
	private static final String TAG = "supermanager";
	
	// players new team XPATH
	private static final String PLAYERS_NEW_TEAM_XPATH = "//table";
	
	public ArrayList<Player> parse(String html, Integer jornada) throws Exception
	{
		ArrayList<Player> players = new ArrayList<Player>();
		
		HtmlCleaner cleaner = new HtmlCleaner();
		TagNode node = cleaner.clean(html);
		
		// cerquem les jugadors
		Object[] table = node.evaluateXPath(PLAYERS_NEW_TEAM_XPATH);
		
		// si estan tancats els canvi, retorna pagina buida i no tenim players
		if( table.length == 0 )
			return null;
		
		TagNode tagTable = (TagNode)table[1];
		TagNode tagTbody = tagTable.getChildTags()[0];
		
		// llistem els jugados, diferents trs
		int i=0;
		for(Object objTr: tagTbody.getChildren())
		{
			// saltem tr, header
			if( i++ == 0 )
				continue;
			
			// els tds
			List<?> tds = ((TagNode)objTr).getChildren();
			
			// agafem el codi jugador i l'id per fer l'update
			String cod = null;
			String idJug = null;
			String color = null;
			
			// quart element -> td
			TagNode td3 = (TagNode)tds.toArray()[3];
			TagNode a = null;
			for(Object td3Child: td3.getChildren())
			{
				a = (TagNode)td3Child;
				if( "a".equals(a.getName()) )
				{
					String href = a.getAttributeByName("href");
					cod = href.replace("http://www.acb.com/stspartidojug.php?cod_jugador=", "");
					
					// mirem si es red/orange/black
					for(Object aChild: a.getChildren())
					{
						if( !(aChild instanceof TagNode ) )
							continue;
						TagNode aFont = (TagNode)aChild;
						if( "font".equals(aFont.getName()) )
						{
							color = aFont.getAttributeByName("color");
						}
					}
					break;
				}
			}
			
			// si tenim anchor i no es vermell o taronja, cerquem l'id_jug
			if( a != null && 
				!Supermanager.Market.RED_COLOR.equals(color.toUpperCase()) && 
				!Supermanager.Market.ORANGE_COLOR.equals(color.toUpperCase()) )
			{
				// nove element -> td del form, el HtmlCleaner formateja be i posa el td enlloc del form
				TagNode tdFormNode = (TagNode)tds.toArray()[9];

				// cerquem input de id_jug
				List<?> inputChilds = tdFormNode.getChildren();
				
				TagNode input = (TagNode)inputChilds.toArray()[1];
			
				// si es el element q toca
				if( input.getAttributeByName("name").equals("id_jug"))
					idJug = input.getAttributeByName("value");
			}
			
			Log.d(TAG,"MarketBuyParser id_jug: "+idJug+", parsing code player: "+cod+", jornada: "+jornada);
			
			// creem player i el guardem per despres tractar-lo
			Player player = new Player();
			player.setDay(Integer.valueOf(jornada));
			player.setCodi(cod);
			player.setId(idJug);
			player.setShowInBuy(true);
			player.setColorInBuy(color);
			
			// guardem player al array list
			players.add(player);
		}
		
		return players;
	}
}
